<?php

/**
 * Description of Deal
 *
 * @author Vu
 */
class Deal extends Zend_Db_Table_Abstract {

    protected $_name = 'cms_deal';
    protected $_primary = 'deal_id';

    public function getAllDeals($page = 1, $order = "d.created desc", $filter = "") {
        try{
        $tb = Zend_Db_Table::getDefaultAdapter();
        $tb->setFetchMode(Zend_Db::FETCH_OBJ);

        $select = $tb->select()
                        ->from(array('d' => 'cms_deal'))
                        ->joinLeft(array('c' => 'cms_category'), 'c.category_id = d.category_id')
                        ->where("d.deal_intro LIKE '%$filter%' OR d.deal_title LIKE '%$filter%'")
                        ->order($order);
        $paginator = Zend_Paginator::factory($select);
        $paginator->setItemCountPerPage(Zend_Registry::get('page_size'));
        
        $paginator->setCurrentPageNumber($page);
        }
        catch(Exception $ex){
            echo $ex->getMessage();
            die;
        }
        return $paginator;
    }
    
    public function getDeals($page = 1, $order = "b.deadline desc", $filter = "")
    {
    	try{
        $tb = Zend_Db_Table::getDefaultAdapter();
        $tb->setFetchMode(Zend_Db::FETCH_OBJ);

        $select = $tb->select()
                ->from(array('a'=>'cms_order_item'), array('count_deal' => 'SUM(a.quantity)'))
                ->join(array('b'=>'cms_deal'),'a.deal_id = b.deal_id')
                ->where("b.deal_title LIKE '%$filter%'")
                ->group('b.deal_id')
                ->order($order);
        $paginator = Zend_Paginator::factory($select);
        $paginator->setItemCountPerPage(Zend_Registry::get('page_size'));
        
        $paginator->setCurrentPageNumber($page);
        }
        catch(Exception $ex){
            echo $ex->getMessage();
            die;
        }
        return $paginator;
	}

    public function getAllPublishedDeals($page = 1) {
        $tb = Zend_Db_Table::getDefaultAdapter();
        $tb->setFetchMode(Zend_Db::FETCH_OBJ);

        $select = $tb->select()->from('cms_deal')->where('status = 1 and deadline > now()')->order(array('deadline ASC', 'created DESC'));
        $paginator = Zend_Paginator::factory($select);
        $paginator->setItemCountPerPage(Zend_Registry::get('page_deal_size'));
        $paginator->setCurrentPageNumber($page);

        return $paginator;
    }

    public function getAllSoldDeals($page = 1) {
        $tb = Zend_Db_Table::getDefaultAdapter();
        $tb->setFetchMode(Zend_Db::FETCH_OBJ);

        $select = $tb->select()->from('cms_deal')->where('deadline < now()')->order('created');
        $paginator = Zend_Paginator::factory($select);
        $paginator->setItemCountPerPage(Zend_Registry::get('page_sold_deal_size'));
        $paginator->setCurrentPageNumber($page);

        return $paginator;
    }

    public function getDealById($dealid) {
        $row = $this->fetchRow($this->select()->where('deal_id=?', $dealid));

        return $row;
    }

    public function getHotDeal() {
        $row = $this->fetchRow($this->select()->where('deadline > now()')->order('created'));

        return $row;
    }

}